package com.internetCafes.spms.web.customer.service.impl.customer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.internetCafes.spms.web.customer.entity.customer.CustomerCooperationDetail;
import com.internetCafes.spms.web.customer.mapper.customer.CustomerCooperationDetailMapper;
import com.internetCafes.spms.web.customer.model.customer.cooperation.CooperationDetailReq;
import com.internetCafes.spms.web.customer.service.customer.ICustomerCooperationDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sms.common.util.hutool.core.bean.BeanUtil;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户合作记录明细 服务实现类
 * </p>
 *
 * @author Zj
 * @since 2021-11-19
 */
@Service
public class CustomerCooperationDetailServiceImpl extends ServiceImpl<CustomerCooperationDetailMapper, CustomerCooperationDetail> implements ICustomerCooperationDetailService {

    @Override
    public boolean saveCooperationDetailList(List<CooperationDetailReq> incomeDetailList, Long cooperationId) {
        remove(
                new LambdaQueryWrapper<CustomerCooperationDetail>()
                        .eq(CustomerCooperationDetail::getCooperationId, cooperationId));

        // 保存合作记录明细列表
        if (CollectionUtil.isNotEmpty(incomeDetailList)) {
            List<CustomerCooperationDetail> detailBatchDo = incomeDetailList.stream()
                    .map(detailReq -> {
                        CustomerCooperationDetail detailDo = new CustomerCooperationDetail();
                        BeanUtil.copyProperties(detailReq, detailDo);
                        detailDo.setCooperationId(cooperationId);
                        return detailDo;
                    }).collect(Collectors.toList());
            saveBatch(detailBatchDo);
        }
        return true;
    }
}
